package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

//本类是用来测试JDBC查询数据库的演示demo  查询location表中的所有数据
public class TestSelect {
    private static Connection conn = null;
    private static PreparedStatement ps = null;
    public static void main(String[] args) throws Exception {
        //1.加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.获取和数据库的链接
        //2.1指定URL 确定要链接的是那个数据库
        String url = "jdbc:mysql://localhost:3306/tedu?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
        //2.2指定用户名和密码
        String user = "root";
        String psd = "root";
        //2.3调用DriverManager的getConnection方法获取链接
        conn = DriverManager.getConnection(url, user, psd);

        String sql = "SELECT id,name FROM location";

        ps = conn.prepareStatement(sql);

        ResultSet rs = ps.executeQuery();

        while (rs.next()){
            int id = rs.getInt("id");
            String name = rs.getString("name");
            System.out.println(id + "\t" + name);
        }
        rs.close();
        ps.close();
        conn.close();

    }
}
